package com.study.spring.ch03.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.study.spring.ch03.pojo.User;

@Controller
@RequestMapping(value = "/ch03/params") // ①处理器的通用映射前缀
public class ParamsController {

	/**
	 * 发出请求：http://localhost:8080/ch03/params/webreq?test=abc
	 * 
	 * @param webRequest
	 * @param nativeWebRequest
	 * @return
	 */
	@RequestMapping("/webreq")
	public String webRequest(WebRequest webRequest, NativeWebRequest nativeWebRequest) {
		// System.out.println("..");
		System.out.println(webRequest.getParameter("test"));// ①得到请求参数test的值
		webRequest.setAttribute("name", "value", WebRequest.SCOPE_REQUEST);// ②
		System.out.println(webRequest.getAttribute("name", WebRequest.SCOPE_REQUEST));
		HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);// ③
		HttpServletResponse response = nativeWebRequest.getNativeResponse(HttpServletResponse.class);

		String username = request.getParameter("test");
		request.setAttribute("username", username);
		String password = request.getParameter("password");
		request.setAttribute("password", password);
		return "ch03/login";
	}

	/**
	 * 
	 * @param request
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String addUser(HttpServletRequest request, User user) {
		System.out.println(user.getUsername());
		System.out.println(user.getPassword());
		return "index.jsp";
	}
	
	/**
	 * 
	 * @param request
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String createUser(HttpServletRequest request, User user) {
		System.out.println(user.getUsername());
		System.out.println(user.getPassword());
		return "ch03/index";
	}
	
	/**
	 * @param model
	 * @param model2
	 * @param model3
	 * @return
	 */
	@RequestMapping("/maps")
	public String createUser(Model model, Map model2, ModelMap model3) {
		  model.addAttribute("a", "aa");
		  model2.put("b", "b1");
		  model3.put("c", "c2");
		  System.out.println(model == model2);
		  System.out.println(model2 == model3);
		  return "ch03/index";
		}
	
	@RequestMapping("/session1")
	public String session(HttpSession session) {
		System.out.println(session);
		return "ch03/index";
	}
	
	

}
